<?php
/**
 *	KLF Framework
 *
 *	Written by Kristian Oye <kristianoye@gmail.com> on Nov 20, 2011
 *	Copyright (C) 2010-2011
 */
namespace KLF\System\Cache
{
	/**
	 *  Class KFakeCache
	 *
	 *  A placeholder for a system that does not have a cache system.
	 *
	 *  @package KLF\System\Cache
	 *  @access public
	 *  @author Kristian Oye <kristianoye@gmail.com>
	 *  @since 0.1
	 *  
	 *  @property Cache array
	 *
	 *  @method KFakeCache NewInstance
	 */
	class KFakeCache extends KCache implements ICache
	{
		/**
		 * The data that would have/should have gone into a real cache.
		 * 
		 * @var array $_rpropCache
		 */
		protected $_rpropCache;
		
		/**
		 * Constructs a false cache object.
		 */
		public function __construct()
		{
			$this->_rpropCache = array();
		}
		
		/**
		 * Adds a single value to the cache.
		 * 
		 * @see KLF\System\Cache.ICache::AddValue()
		 */
		public function AddValue($key, $val, $expires=0, $flag=NULL)
		{
			$this->_rpropCache[$key] = array(
							"value" => $val, 
							"expires" => $expires > 0 ? time() + $expires : 0); 
		}
		
		/**
		 * Deletes one or more values from the cache.
		 * 
		 * @see KLF\System\Cache.ICache::DeleteValues()
		 */
		public function DeleteValues()
		{
			foreach(func_get_args() as $key)
			{
				unset($this->_rpropCache[$key]);
			}
		}
		
		/**
		 * Clears all the values out of the cache.
		 * 
		 * @see KLF\System\Cache.ICache::Flush()
		 */
		public function Flush()
		{
			$this->_rpropCache = array();
		}
		
		/**
		 * Returns a single value from the cache.
		 * 
		 * @param string $key
		 */
		public function GetValue($key)
		{
			return isset($this->_rpropCache[$key]) ? 
				$this->_rpropCache[$key]["value"] : NULL;
		}
		
		/**
		 * Returns multiple values from the cache.
		 */
		public function GetValues()
		{
			$result = array();
			foreach (func_get_args() as $key) {
				$result[$key] = $this->GetValue($key);
			}
			return $result;
		}
		
		/**
		 * Initialize the fake cache object.
		 * 
		 * @see KLF\System\Cache.ICache::Initialize()
		 */
		public function Initialize()
		{
		}
		
		/**
		 * Sets a value in the cache.
		 * 
		 * @param string $key
		 * @param mixed $val
		 */
		public function SetValue($key, $val, $expires=0)
		{
			$this->_rpropCache[$key] = array(
				"value" => $val, 
				"expires" => $expires > 0 ? time() + $expires : 0); 
		}
	}
}